package com.archerdu.apply.controller.front;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Article;
import com.archerdu.apply.model.ArticleCatalog;
import com.archerdu.apply.service.ArticleCatalogManager;
import com.archerdu.apply.service.ArticleManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.dao.IPage;
import com.archerdu.commons.constant.DataConstant;

@Controller(value = "frontArticleController")
@Scope("prototype")
@RequestMapping("/news")
public class ArticleController extends BaseController {

    private static final long  serialVersionUID = 3298072203679257178L;

    private ArticleCatalogManager articleCatalogManager;
    private ArticleCatalog        articleCatalog;
    private ArticleManager     articleManager;
    private Article            article;

    @RequestMapping(value = "")
    public ModelAndView page(Integer pageNo, Integer pageSize) {
        article = new Article();
        return page(article, pageNo, pageSize);
    }

    @RequestMapping(value="/c{cateId}")
    public ModelAndView page(@PathVariable String cateId, Integer pageNo, Integer pageSize){
        article = new Article();
        article.setCatalogId(cateId);
        return page(article, pageNo, pageSize);
    }
    
    public ModelAndView page(Article article, Integer pageNo, Integer pageSize){
        if (null == pageNo || null == pageSize) {
            pageNo = 1;
            pageSize = 20;
        }
        logger.debug("articleManager is " + articleManager);
        article.setUseFlag(DataConstant.COMMON_USE_FLAG_YES);
        IPage<Article> page = articleManager.pageSearch(article, pageNo, pageSize);

        ModelAndView mav = new ModelAndView();
        mav.addObject(article);
        mav.addObject(page).setViewName("front/article/page");
        return mav;
    }
    
    
    @RequestMapping(value="/{id}")
    public ModelAndView info(@PathVariable String id){
        ModelAndView mav = new ModelAndView();
        article = articleManager.get(id);
        mav.addObject("article", article)
            .setViewName("front/article/info");
        return mav;
    }
    
    public ArticleCatalogManager getArticleCatalogManager() {
        return articleCatalogManager;
    }

    public void setArticleCatalogManager(ArticleCatalogManager articleCatalogManager) {
        this.articleCatalogManager = articleCatalogManager;
    }

    public ArticleCatalog getArticleCatalog() {
        return articleCatalog;
    }

    public void setArticleCatalog(ArticleCatalog articleCatalog) {
        this.articleCatalog = articleCatalog;
    }

    public ArticleManager getArticleManager() {
        return articleManager;
    }

    public void setArticleManager(ArticleManager articleManager) {
        this.articleManager = articleManager;
    }

    public Article getArticle() {
        return article;
    }

    public void setArticle(Article article) {
        this.article = article;
    }
}
